package com.kanshu.ksgb.fastread.doudou.module.bookcity.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.FinishedAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.WrapBookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.SelectedRequestParams;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wcy on 18/5/25.
 */
@Route(path = ARouterConfig.NEW_BOOK_OR_ATTRACT)
public class NewBookOrAttractActivity extends BaseActivity implements INetCommCallback<List<WrapBookInfo>> {
    public static final String EXTRA_TYPE_PARAMS = "type";
    @BindView(R.id.recyler_view)
    RecyclerView mRecylerView;
    @BindView(R.id.empty_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;
    BookCityPresenter mPresenter;
    PageRequestParams mRequestParams;
    FinishedAdapter mFinishedAdapter;
    List<WrapBookInfo> mBookInfos = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_common_refresh_rv);
        ButterKnife.bind(this);
        String type = getIntent().getStringExtra(EXTRA_TYPE_PARAMS);
        mRequestParams = new SelectedRequestParams();
        ((SelectedRequestParams)mRequestParams).type_name = type;
        if (Constants.BookCityModuleType.TYPE_SELECTED_ZHONGJIYOUHUO.equals(type)) {
            setTitle("终极诱惑");
        } else if (Constants.BookCityModuleType.TYPE_SELECTED_XINSHUQIANGXIAN.equals(type)) {
            setTitle("新书抢先");
        }
        init();
    }

    public void dismissLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    private void init() {
        mPresenter = new BookCityPresenter(lifeCyclerSubject);
        SwipeRefreshHelper.init(mSwipeRefresh, () -> {
            mRequestParams.page = 1;
            mPresenter.getNewBookOrAttractBooks(mRequestParams, this);
        });
        mFinishedAdapter = new FinishedAdapter(this, mBookInfos);
        mFinishedAdapter.setType(FinishedAdapter.TYPE_TWO);
        RecyclerViewHelper.initRecyclerViewV(this, mRecylerView, mFinishedAdapter);
        mFinishedAdapter.setRequestDataListener(() -> {
            mPresenter.getNewBookOrAttractBooks(mRequestParams, this);
        });
        mPresenter.getNewBookOrAttractBooks(mRequestParams, this);
        mEmptyLayout.setRetryListener(new EmptyLayout.OnRetryListener() {
            @Override
            public void onRetry() {
                mRequestParams.page = 1;
                mPresenter.getNewBookOrAttractBooks(mRequestParams, NewBookOrAttractActivity.this);
            }
        });
    }
    @Override
    public void onResponse(List<WrapBookInfo> wrapBookInfos) {
        dismissLoading();
        if (mRequestParams.page == 1) {
            if (Utils.isEmptyList(wrapBookInfos) && mBookInfos.size() <= 0) {
                //mEmptyLayout.setEmptyMessage("暂无记录");
                if (mEmptyLayout == null) {
                    return;
                }
                showEmptyByCode(Constants.ErrCode.NoData);
                SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mFinishedAdapter);
                return;
            }
            mBookInfos.clear();
            mFinishedAdapter.setIsNoMoreData(false);
        }
        mBookInfos.addAll(wrapBookInfos);
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mFinishedAdapter);
        if (mRequestParams.page >= wrapBookInfos.get(0).totalPage) {
            mFinishedAdapter.noMoreData();
        } else {
            mRequestParams.page++;
        }
        mFinishedAdapter.notifyDataSetChanged();

    }

    @Override
    public void onError(int code, String errDesc) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mFinishedAdapter);
        if (Utils.isEmptyList(mBookInfos)) {
            showEmptyByCode(code);
        }
    }

    public void showEmptyByCode(int code) {
        int status = EmptyLayout.STATUS_NO_NET;
        if (code == Constants.ErrCode.NoData) {
            status = EmptyLayout.STATUS_NO_DATA;
        }
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(status);
        }
    }
}
